package com.superpeer.baseproject.wight;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.Spanned;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.superpeer.baseproject.R;

/**
 * description:自定义dialog
 */

public class CustomDialog extends Dialog {
    /**
     * 显示的消息
     */
    private TextView messageTv ;

    /**
     * 确认和取消按钮
     */
    private Button negativeBn ,positiveBn;

    /**
     * 按钮之间的分割线
     */
    private View columnLineView ;
    private LinearLayout ll_content;
    public CustomDialog(Context context) {
        super(context, R.style.CustomDialog);
    }

    /**
     * 都是内容数据
     */
    private String message;
    private Spanned spanned;
    private String positive,negative ;
    private View view;

    private OnPositiveClickListener onPositiveClickListener;
    private OnNegativeClickListener onNegativeClickListener;
    private CustomDialog customDialog;

    private boolean autoDismiss = true;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.item_custom_dialog);

        customDialog = this;

        //初始化界面控件
        initView();
        //初始化界面数据
        refreshView();
        //初始化监听
        initListener();

//        //默认点击空白处是否取消
//        setCanceledOnTouchOutside(true);
//        //默认点击返回可以取消
//        setCancelable(true);
    }

    //点击按钮是否自动取消
    public CustomDialog setAutoDismiss(boolean autoDismiss){
        this.autoDismiss = autoDismiss;
        return this;
    }

    private void initListener() {
        negativeBn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(null != onNegativeClickListener){
                    onNegativeClickListener.click(CustomDialog.this);
                }
                if(autoDismiss){
                    if(customDialog.isShowing()){
                        customDialog.dismiss();
                    }
                }
            }
        });
        positiveBn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(null != onPositiveClickListener){
                    onPositiveClickListener.click(customDialog);
                }
                if(autoDismiss){
                    if(customDialog.isShowing()){
                        customDialog.dismiss();
                    }
                }
            }
        });
    }

    /**
     * 初始化界面的确定和取消监听器
     */

    /**
     * 初始化界面控件的显示数据
     */
    private void refreshView() {
        //如果用户自定了message
        if (!TextUtils.isEmpty(message)) {
            messageTv.setText(message);
        }
        if(!TextUtils.isEmpty(spanned)){
            messageTv.setText(spanned);
        }
        //如果设置按钮的文字
        if (!TextUtils.isEmpty(positive)) {
            positiveBn.setText(positive);

            positiveBn.setVisibility(View.VISIBLE);
        }else {
            positiveBn.setText("确定");
        }

        if (!TextUtils.isEmpty(negative)) {
            negativeBn.setText(negative);

            negativeBn.setVisibility(View.VISIBLE);
        }else {
            negativeBn.setText("取消");
        }

        if(!TextUtils.isEmpty(positive) && !TextUtils.isEmpty(negative)){
            columnLineView.setVisibility(View.VISIBLE);
        }
        if(null != view){
            ll_content.removeAllViews();
            ll_content.addView(view);
        }
    }

    @Override
    public void show() {
        super.show();
    }

    /**
     * 初始化界面控件
     */
    private void initView() {
        negativeBn = (Button) findViewById(R.id.negative);
        positiveBn = (Button) findViewById(R.id.positive);
        messageTv = (TextView) findViewById(R.id.message);
        columnLineView = findViewById(R.id.column_line);
        ll_content = findViewById(R.id.ll_content);
    }

    public CustomDialog setView(View view){
        this.view = view;
        return this;
    }

    public CustomDialog setMessage(String message){
        this.message = message;
        return this;
    }

    public CustomDialog setMessage(Spanned spanned){
        this.spanned = spanned;
        return this;
    }

    public CustomDialog setPositiveButton(String positive, OnPositiveClickListener listener){
        this.positive = positive;
        this.onPositiveClickListener = listener;
        return this;
    }

    public CustomDialog setNegativeButton(String negative, OnNegativeClickListener listener){
        this.negative = negative;
        this.onNegativeClickListener = listener;
        return this;
    }

    public interface OnPositiveClickListener{
        void click(CustomDialog dialog);
    }

    public interface OnNegativeClickListener{
        void click(CustomDialog dialog);
    }

}

